package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.data.CategoryData;

import java.util.List;

/**
 * Created by Administrator on 2016/9/12.
 */
public class SearchCarAdapter extends BaseAdapter {

    private List<CategoryData.SearchResult> mResultSet;

    private Context mContext;

    public LayoutInflater mInflater;

    public SearchCarAdapter(List<CategoryData.SearchResult> resultSet, Context context) {
        mResultSet = resultSet;
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mResultSet.size();
    }

    @Override
    public Object getItem(int position) {
        return mResultSet.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setData(List<CategoryData.SearchResult> resultSet){
        this.mResultSet = resultSet;
        notifyDataSetChanged();
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SearchResultViewHolder holder;
        if (convertView == null) {
            convertView = mInflater
                    .inflate(R.layout.list_search_item, null);
            holder = new SearchResultViewHolder();
            holder.resultText = (TextView) convertView
                    .findViewById(R.id.modelText);
            holder.numsTextView = (TextView) convertView
                    .findViewById(R.id.nums);
            convertView.setTag(holder);
        } else {
            holder = (SearchResultViewHolder) convertView.getTag();
        }
        if (mResultSet.get(position).mIsBrand) {
            holder.resultText.setText(mResultSet.get(position).mBrandName);
        } else {
            holder.resultText.setText(mResultSet.get(position).mModelName);
        }
        holder.numsTextView.setText("大约" + mResultSet.get(position).nums + "个结果");
        return convertView;

    }

    public final class SearchResultViewHolder {
        public TextView resultText;
        public TextView numsTextView;
    }

}